package com.ruoyi.client.controller;

import cn.hutool.core.lang.Assert;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.ruoyi.client.service.ClientUserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supply.domain.TMember;
import com.ruoyi.supply.enums.MemberConsumeTypeEnum;
import com.ruoyi.supply.enums.MemberGradeEnum;
import com.ruoyi.supply.service.impl.TMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author huangxiong
 * @date 2025/06/03 20:42
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/client/user")
@Api(tags = "客户端用户模块")
@Slf4j
public class ClientUserController {

    public final ClientUserService clientUserService;

    public final TMemberService tMemberService;

    /**
     * 登录方法
     * @param jsCode sessionKey
     * @return 结果
     */
    @GetMapping("/login")
    @Anonymous
    @ApiOperation("登陆")
    public AjaxResult login(@RequestParam("jsCode") String jsCode)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = clientUserService.login(jsCode);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @ApiOperation("解密微信手机号")
    @PostMapping("/phone")
    public AjaxResult getPhoneNumber(@RequestBody Map<String, String> request) {
        String code = request.get("code");
        String encryptedData = request.get("encryptedData");
        String iv = request.get("iv");
        String phone = clientUserService.getWxPhone(code,encryptedData,iv);
        return AjaxResult.success("success",phone);
    }


    /**
     * 获取会员管理详细信息
     */
    @GetMapping
    @ApiOperation("获取会员管理详细信息")
    public AjaxResult getInfo()
    {
        return AjaxResult.success(tMemberService.selectTMemberById(SecurityUtils.getUserId()));
    }

}

